package com.mall.b2c.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.mall.b2c.dao.StockNoticeDao;
import com.mall.b2c.domain.goods.GoodsDomain;
import com.mall.b2c.domain.member.MemberDomain;
import com.mall.b2c.domain.member.StockNoticeDomain;
import com.mall.b2c.service.GoodsService;
import com.mall.b2c.service.MemberService;
import com.mall.b2c.service.StockNoticeService;
import com.mall.common.exception.NoticeException;
import com.mall.common.hibernate.Finder;
import com.mall.common.service.impl.BaseServiceImpl;


/**
 * Service实现类 - 到货通知
 * @author lzx
 */
@Service("b2cStockNoticeService")
@Transactional
public class StockNoticeServiceImpl extends BaseServiceImpl<StockNoticeDomain, Long> implements StockNoticeService {
	
	@Resource
	public void setBaseDao(StockNoticeDao stockNoticeDao) { 
		super.setBaseDao(stockNoticeDao); 
	}

	@Resource private MemberService memberService;
	@Resource private GoodsService goodsService;
	
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void register(Long goodsId, Integer quantity, String memo, Long memberId) {
		
		MemberDomain member = memberService.find(memberId);
		if(member == null){
			throw new NoticeException("帐号不存在");
		}
		
		GoodsDomain goods = goodsService.find(goodsId);
		if(goods == null){
			throw new NoticeException("商品不存在");
		}
		
		if(goods.getStock() != null && goods.getStock() > 1){
			throw new NoticeException("该商品有货无需登记");
		}
		
		StockNoticeDomain stockNotice = isRegister(goodsId, memberId);
		if(stockNotice != null){
			if(stockNotice.getIsNotice()){
				stockNotice.setIsNotice(false);
				if(quantity != null && quantity > 0){
					stockNotice.setNum(quantity);
				}else{
					stockNotice.setNum(1);
				}

				if(StringUtils.isNotBlank(memo)){
					stockNotice.setMemo(memo);
				}
				this.update(stockNotice);
			}else{
				throw new NoticeException("该商品已经登记过了");
			}
		}else{
			stockNotice = new StockNoticeDomain();
			stockNotice.setGoods(goods);
			stockNotice.setMember(member);
			stockNotice.setMemo(memo);
			if(quantity != null && quantity > 0){
				stockNotice.setNum(quantity);
			}
			this.persist(stockNotice);
		}
		
	}
	
	/**
	 * 是否已登记
	 * @param goodsId
	 * @param memberId
	 * @return
	 */
	private StockNoticeDomain isRegister(Long goodsId, Long memberId){
		Finder finder = Finder.create("from StockNoticeDomain bean where bean.member.id = :memberId and bean.goods.id =:goodsId");
		finder.setParam("memberId", memberId);
		finder.setParam("goodsId", goodsId);
		Object obj = finder.findUniqueByHQL();
		if(obj != null){
			return (StockNoticeDomain) obj;
		}
		return null;
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void delete(Long[] ids, Long memberId) {
		List<StockNoticeDomain> stockNotices = this.get(ids);
		if(CollectionUtils.isEmpty(stockNotices)){
			throw new NoticeException("记录不存在");
		}
		for(StockNoticeDomain stockNotice : stockNotices){
			if(stockNotice.getMember() != null 
					&& stockNotice.getMember().getId().equals(memberId)){
				this.remove(stockNotice);
			}
		}
	}

}
